package com.primary.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.primary.common.CommonStr;
import com.primary.common.MD5Utils;
import com.primary.controller.dto.ChangePasswordDto;
import com.primary.controller.request.UsersRequest;
import com.primary.entity.Users;
import com.primary.mappers.UsersMapper;
import com.primary.service.UsersService;
import com.primary.utils.StringReplaceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("UsersService")
public class UsersServiceImpl implements UsersService{

    @Autowired
    UsersMapper usersMapper;

    /**
     * @author Shen Jianhui
     * @date 2023/06/18 Sunday
     * @updatetime 2023/07/16 Sunday
     * @updatetime 2023/08/20 Sunday
     * @params name, password
     * @description For Signing in to query name and password
     * @updateDescription(20230716) Added a method to encrypt password
     * @updateDescription(20230820) Added a method that to replace some characters in string to make sure phone and address privately
     */
    @Override
    public Users getUserByNameAndPassword(String name, String password) {
        Users users = usersMapper.getUserByNameAndPassword(name, MD5Utils.inputPassToFormPass(password));
        try{
            if(users!=null){
                users.setPhone(StringReplaceUtils.stringPhoneReplace(users.getPhone()));
                users.setAddress(StringReplaceUtils.stringAddressReplace(users.getAddress()));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return users;
    }

    @Override
    public List<Users> allusers() {
        return usersMapper.allusers();
    }

    /**
     * @param users Object
     * @author Shen Jianhui
     * @date 2023/06/18 Sunday
     * @updateTime 2023/07/24 Monday
     * @description For Signing in to insert users' information
     * @updateDescription To avoid insert one/some phone(s) and one/some email(s) which/that is/are existed
     */
    @Override
    public int registerByNameAndPwd(Users users) {
        //At the same time, if this phone or email is existing, this information that input by user won't could be inserted
        //Because of the method of register in the usersMapper.java will return an int number, so there is nine to choose, and it is meaningless
        if (usersMapper.getPhoneAndEmailForRegister(users.getPhone(), users.getEmail()) != null) {
            return CommonStr.NUM_NINE;
        }
        users.setPassword(MD5Utils.inputPassToFormPass(users.getPassword()));
        return usersMapper.registerByNameAndPwd(users);
    }

    @Override
    public Users getNameForRegister(String name) {
        return usersMapper.getNameForRegister(name);
    }

    /**
     * @author Shen Jianhui
     * @date 2023/07/16 Sunday
     * @description To insert a users' information in this table, this method will check the username
     *              is existing or not, then it will return the affected rows, usually it is one or zero
     */
    @Override
    public int insertOneUser(Users users) {
        return usersMapper.getNameForRegister(users.getName()) != null ?
                CommonStr.NUM_ZERO : (usersMapper.insertOneUser(users) != CommonStr.NUM_ZERO ? CommonStr.NUM_ONE : CommonStr.NUM_ZERO);
    }

    /**
     * @author Shen Jianhui
     * @date 2023/07/16 Sunday
     * @description To update a users' information in this table, this method will check the username
     *              is existing or not, then it will return the affected rows, usually it is one or zero
     */
    @Override
    public int updateOneUserById(Users users) {
        users.setUpdateTime(new Date());
//        return usersMapper.getNameForRegister(users.getName()) != null ? 0 : (usersMapper.updateOneUserById(users) != 0 ? 1 : 0);
        return usersMapper.updateOneUserById(users) != CommonStr.NUM_ZERO ? CommonStr.NUM_ONE : CommonStr.NUM_ZERO;
    }

    /**
     * @author Shen Jianhui
     * @date 2023/07/16 Sunday
     * @description This method will delete a users' information from table by the param id
     */
    @Override
    public boolean deleteUserById(Integer id) {
        return usersMapper.deleteUserById(id) > 0;
    }

    /**
     * @author Shen Jianhui
     * @date 2023/07/18 Tuesday
     * @description This method will provide System to show data by pages
     * @updatedate 2023/12/24 Sunday
     * @updatedescrpiption BaseRequest -> UsersRequest
     */
    @Override
    public PageInfo<Map<String, Object>> queryByPage(UsersRequest usersRequest) {
        PageHelper.startPage(usersRequest.getPageNum(), usersRequest.getPageSize());
        return new PageInfo<>(usersMapper.listByPage(usersRequest));
    }

    /**
     * @author Shen Jianhui
     * @date 2023/10/24 Tuesday
     * @description This method will provide System to change users' password
     */
    @Override
    public int changePassword(ChangePasswordDto users) {
        users.setPassword(MD5Utils.inputPassToFormPass(users.getPassword()));
        return usersMapper.changePassword(users);
    }
}
